package com.coollang.smartbra.presenter2;

import android.net.Uri;

import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.beans.NewActionData;
import com.coollang.smartbra.bleutil.BleUtil;
import com.coollang.smartbra.bleutil.callback.BleCallbackImpl;
import com.coollang.smartbra.constant.Constants;
import com.coollang.smartbra.constant.ResourceList;
import com.coollang.smartbra.interfaces.ActionFragmentListener;
import com.coollang.smartbra.interfaces.ActionType;
import com.coollang.smartbra.interfaces.NewActionView;
import com.coollang.smartbra.model.NewActionModel;
import com.coollang.smartbra.utils.Command;
import com.coollang.smartbra.utils.TimeUtils;

import java.util.ArrayList;

/**
 * @author: Timmy
 * @data: 2017/12/14
 * @Fuction: 新动作页面的model 层
 */
public class NewActionPresenter extends AbstractMvpPresenter<NewActionView> {

    private static final String TAG = "NewActionPresenter";
    private NewActionModel mNewActionModel;
    private long mStartTime;
    private long mEndTime;

    /**
     * 标准数,当小于这个数动作不计数
     */
    private static final int STANDARD_COUNT = 10;

    public NewActionPresenter() {
        mNewActionModel = new NewActionModel();
        BleUtil.getInstance().registerNotifyListener(mBleCallback);
        // 开启心率开关
        BleUtil.getInstance().sendData((byte) 0x47);
    }

    /**
     * @return 获取视频播放地址
     */
    public Uri getVideoPath(int actionType) {
        int actionListVideo = ResourceList.ACTION_LIST_VIDEOS[actionType];
        String packageName = MyApplication.getApplication().getPackageName();
        return Uri.parse("android.resource://" + packageName + "/" + actionListVideo);
    }

    private boolean mIsSporting;

    private ActionFragmentListener mListener;

    public void setListener(ActionFragmentListener listener) {
        mListener = listener;
    }

    /**
     * 开始运动
     */
    public void startAction() {
        if (mListener != null) {
            mStartTime = TimeUtils.getCurrentTimeUnixLong();
            // 发送动作开始的命令
            byte data = Command.ACTION_COMMAND[getmMvpView().getActionType()];
            BleUtil.getInstance().sendData(data);
            mListener.actionStart();
            mIsSporting = true;
        }
    }

    private ArrayList<NewActionData> mNewActionDatas;

    public ArrayList<NewActionData> getNewActionDatas() {
        return mNewActionDatas;
    }

    public void finishAction(ArrayList<NewActionData> actionDatas) {
        mNewActionDatas = actionDatas;
        int count = 0;
        int time = 0;
        int actionType = 0;

        for (NewActionData actionData : actionDatas) {
            actionType = actionData.getActionType();
            int actionFlag = actionData.getActionFlag();

            if (actionFlag == 1) {
                count++;
                time += actionData.getTime();
            }
        }

        if (Constants.ACTION_NAME_ID1[actionType] == ActionType.PBZC) {
            if (time <= 10000) {
                getmMvpView().showFinishDialog();
            } else {
                stopAction();
                getmMvpView().finishActivity();

            }

        } else {
            if (count < STANDARD_COUNT) {
                getmMvpView().showFinishDialog();
            } else {
                stopAction();
                getmMvpView().finishActivity();
            }
        }
    }

    public void stopAction() {
        mEndTime = TimeUtils.getCurrentTimeUnixLong();
        byte[] data = {Command.ACTION_COMMAND[getmMvpView().getActionType()], 0x02};
        BleUtil.getInstance().sendData(data);
        mIsSporting = false;
    }

    public boolean isSporting() {
        return mIsSporting;
    }

    /**
     * 蓝牙数据监听
     */
    private BleCallbackImpl mBleCallback = new BleCallbackImpl() {
        @Override
        protected void realCountData(int heartRate, int errorFlag, int errorType) {
            if (mListener != null) {
                mListener.realCountData(heartRate, errorFlag, errorType);
            }
        }

        @Override
        public void onActionZhengFan(byte zhengfan, byte qingxie) {
            getmMvpView().onActionZhengFan(zhengfan, qingxie);
        }
    };

    @Override
    public void detachMvpView() {
        BleUtil.getInstance().sendData((byte) 0x47, (byte) 02);
        stopAction();
        BleUtil.getInstance().unRegisterNotifyListener();
        mBleCallback = null;
        super.detachMvpView();
    }
}
